package UILayer;
import ModelLayer.*;
import CtrlLayer.*;
import java.util.*;

public class DeliveryUI
{
    DeliveryController deliveryController;
    public DeliveryUI()
    {
        deliveryController = new DeliveryController();
    }

    public void deliveryMenuStart(int sessionId)
    {

        boolean exit=false;
        while (!exit) 
        {
            int choice = writeDeliveryMenu(sessionId);

            switch(choice){
                case 1:
                startCreateDelivery(sessionId);
                break;
                case 2:
                startFindDelivery();
                break;
                case 3:
                startDeleteDelivery();
                break;
                case 0:
                exit = true;
                break;

            }

        }
    }

    private int writeDeliveryMenu(int sessionId)
    {       

        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Delivery Menu ***");
        System.out.println(" (1) Create a delivery");
        System.out.println(" (2) Show a delivery");
        if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2 ){
            System.out.println(" (3) Delete a delivery"); 
        }
        System.out.println(" (0) Back");
        System.out.print("\n Make your choice: ");
        int choice = keyboard.nextInt();
        return choice;
    }

    private void startCreateDelivery(int sessionId)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("###Delivery form:###");
        System.out.println("Enter the ID of the customer:");
        int customerID = Integer.parseInt(keyboard.nextLine());
        System.out.println("Enter the address of delivery:");
        String address = keyboard.nextLine();
        System.out.println("Enter the phone of the customer:");
        String phone = keyboard.nextLine();
        System.out.println("Enter the price for the delivery:");
        double price = Integer.parseInt(keyboard.nextLine());
        int deliveryId = deliveryController.createDelivery(customerID,address,phone,price,sessionId);  
        if(deliveryId != 0) {
            boolean exit = false;
            while(!exit)
            {
               System.out.println(" (1) Add a product");
               System.out.println(" (2) Remove a product");
               System.out.println(" (0) Finish delivery");
               int choice = Integer.parseInt(keyboard.nextLine());

                switch(choice){
                case 1:
                addProduct(deliveryId);
                break;
                case 2:
                removeProduct(deliveryId);
                break;
                case 0:
                exit = true;
                break;
               }
            }

            System.out.println("Delivery with ID: " + deliveryId + " has been finished!");
            
        } else {
            System.out.println("Invalid Customer!");
            deliveryController.deleteDelivery(deliveryId);
        }
        pause();
    }
    
    private void removeProduct(int deliveryId){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Product barcode:");
        int productBarcode = Integer.parseInt(keyboard.nextLine());
        System.out.println("Product amount:");
        int amount = Integer.parseInt(keyboard.nextLine());
        deliveryController.removeProductFromDelivery(deliveryId, productBarcode, amount);
    }

    private void addProduct(int deliveryId){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Product barcode:");
        int productBarcode = Integer.parseInt(keyboard.nextLine());
        System.out.println("Product amount:");
        int amount = Integer.parseInt(keyboard.nextLine());
        boolean check = deliveryController.addProductInDelivery(deliveryId,productBarcode,amount);
        if(!check) {
           System.out.println("There is no such product!");
           deliveryController.removeProductFromDelivery(deliveryId,productBarcode,amount);
        }
    }

    private void startFindDelivery()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Delivery ID:");
        int id = Integer.parseInt(keyboard.nextLine());

        if(deliveryController.findDelivery(id) != null){
            System.out.println();
            System.out.println("ID of delivery: " + deliveryController.findDelivery(id).getDeliveryID());
            System.out.println("Started date: " + deliveryController.findDelivery(id).getDate());
            System.out.println("-----------Products---------------" );

            for (int key : deliveryController.findDelivery(id).getProducts().keySet()) {
                System.out.println("Product: " + key + "\t \t Amount: " + deliveryController.findDelivery(id).getProducts().get(key));
            }
            System.out.println("-----------------------------------" );
            System.out.println("\t \t \t Total: " + deliveryController.findDelivery(id).getPrice());
            System.out.println("-----------------------------------" );
            System.out.println("Employee: " + deliveryController.findDelivery(id).getEmployeeID());
            System.out.println("Customer: " + deliveryController.findDelivery(id).getCustomerID());
        } else {
            System.out.println("The delivery has not been found!");
        }
        pause();
    }

    private void startDeleteDelivery()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("ID");
        int id = Integer.parseInt(keyboard.nextLine());
        deliveryController.deleteDelivery(id);
    }

    private void pause()
    {
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
    }

}
